package com.sl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.sl.entity.PageResult;
import com.sl.entity.Result;
import com.sl.entity.StatusCode;
import com.sl.pojo.Company;
import com.sl.pojo.CompanyCategory;
import com.sl.pojo.CompanyItems;
import com.sl.pojo.CompnayBusiness;
import com.sl.service.CompanyCategoryService;
import com.sl.service.CompanyItemsService;
import com.sl.service.CompanyService;
import com.sl.service.CompnayBusinessService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;
    @Autowired
    private CompanyCategoryService companyCategoryService;
    @Autowired
    private CompnayBusinessService compnayBusinessService;
    @Autowired
    private CompanyItemsService  companyItemsService;


   //查：条件查  带分页  空就是查所有
    @PostMapping("/search")
    public Result findList(@RequestBody String data){
        log.info("多条件搜索公司结果");
        JSONObject jsonObject = JSON.parseObject(data);
        String company = jsonObject.getString("data");
        Map map=  JSON.parseObject(company);
        Integer page = jsonObject.getInteger("page");
        if (null ==page) {page=1;}
        Integer pageSize = jsonObject.getInteger("pageSize");
        if (null ==pageSize) {pageSize = 10;}
        log.info(">>>map" + map +">>>page" + page +">>>pageSize" + pageSize  );
        Page<Company> list = companyService.findList(map,page,pageSize);
        PageResult<Company> companyPageResult = new PageResult<>(list.getTotal(), list.getResult());
        return  new Result(true,StatusCode.OK,"所有公司",companyPageResult);
    }

    //删： 逻辑。。一个企业信息
    @PostMapping("/deleted")
    public Result deletedCompany(@RequestBody String data){
        log.info("逻辑删除公司，deleted为1");
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer id = jsonObject.getInteger("id");
        Company company = companyService.findById(id);
        company.setDeleted(1);
        companyService.updateCompany(company);
        return new Result(true,StatusCode.OK,"逻辑删除企业信息成功");
    }
    //删：真实 。一个企业信息
    @DeleteMapping("/deletedreal")
    public Result deletedReal(@RequestBody String data){
        log.info("删除公司，物理");
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer id = jsonObject.getInteger("id");
        companyService.deletedReal(id);
        return new Result(true,StatusCode.OK,"删除企业信息成功");
    }

    //增：多企业。。那么[]遍历数组。
    @PostMapping("/adds")
    public Result adds(@RequestBody String data){
        if(StringUtils.isEmpty(data)){ return  new Result(false, StatusCode.ERROR,"参数为空");}
        List<Company> companies = JSON.parseArray(data, Company.class);
        for (Company company : companies) {
            log.info("增加公司多个"+company);
            String licenceStatus = company.getLicenceStatus();
            if(licenceStatus.equals(1)){company.setLicenceStatus("正常");}
            else {company.setLicenceStatus("已失效");}
            companyService.addCompany(company);
        }
        return new Result(true,StatusCode.OK,"新增企业信息成功");
    }

    @PostMapping("/update")
    public Result update(@RequestBody String data){
        log.info("更新公司信息"+ data);
        if(StringUtils.isEmpty(data)){ return  new Result(false, StatusCode.ERROR,"参数为空");}
        Company company = JSON.parseObject(data,Company.class);
        companyService.updateCompany(company);
        return new Result(true,StatusCode.OK,"更新企业信息成功");
    }

    /**
     *企业的类别
     * @return
     */
    @GetMapping("/category")
    public Set<String>  findAllCompanyCategory(){
        log.info("查看企业的类别");
        Set<String>  categorySet=new HashSet<>();
        List<CompanyCategory> allCompanyCategory = companyCategoryService.findAllCompanyCategory();
        for (CompanyCategory companyCategory : allCompanyCategory) {
            String category = companyCategory.getCategory();
            categorySet.add(category);
        }
        return categorySet;
    }

    /**
     *查看企业的业务
     * @return
     */
    @GetMapping("/business")
    public Set<String>  findAllCompnayBusiness(){
        log.info("查看企业的业务");
        Set<String>  businessSet=new HashSet<>();
        List<CompnayBusiness> allCompnayBusiness = compnayBusinessService.findAllCompnayBusiness();
        for (CompnayBusiness compnayBusiness : allCompnayBusiness) {
            String business = compnayBusiness.getBusiness();
            businessSet.add(business);
        }
    return businessSet;
    }

    /**
     * 主要经营项目
     * @return
     */
    @GetMapping("/items")
    public Set<String>  findAllItems(){
        log.info("查看企业的主要经营项目");
        Set<String>  itemsSet=new HashSet<>();
        List<CompanyItems> allItems = companyItemsService.findAllItems();
        for (CompanyItems allItem : allItems) {
            String items = allItem.getItems();
            itemsSet.add(items);
        }
        return itemsSet;
    }


}
